package com.synnex.mobile.servier.web.survey.controller.mobile;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.core.util.StringUtil;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.servier.core.survey.dto.SurveyAnswer;
import com.synnex.mobile.servier.core.survey.dto.UserAnswer;
import com.synnex.mobile.servier.core.survey.service.AnswerItemService;
import com.synnex.mobile.servier.core.survey.service.SurveyService;
import com.synnex.mobile.servier.web.common.controller.AbstractController;
import com.synnex.mobile.servier.web.survey.controller.AbstractSurveyController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kerryf
 */
@LogClass(name="mobile survey")
@RequestMapping(url="/mobile/survey")
public class MobileSurveyController extends AbstractSurveyController {

    public static String[] EXCLUDE_FIELDS = null ;

    @RequestMapping(url="/listQuestionnaire.do")
    @LogMethod(name="list questionnaire")
    public ModelAndView getQuestionnaireList(HttpServletRequest request,HttpServletResponse response) {

        try {
            EXCLUDE_FIELDS  = new String[]{"creater","updateDate","entryDate"};
            Map model = super.getQuestionnaires(request);

            JSONModel jsonModel = JSONModel.create().setExcludes(EXCLUDE_FIELDS);
            jsonModel.setData(model);
            return createJSONModelAndView(jsonModel);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url="/listSurvey.do")
     @LogMethod(name="list survey")
     public ModelAndView getSurveyList(HttpServletRequest request,HttpServletResponse response) {

        try {
            EXCLUDE_FIELDS  = new String[]{"creater","updateDate","entryDate","require","question","aseq","atitle","aid","chooseCount","askInfoList"};
            Map model = super.getSurveys(request);

            JSONModel jsonModel = JSONModel.create().setExcludes(EXCLUDE_FIELDS);
            jsonModel.setData(model);
            return createJSONModelAndView(jsonModel);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url="/submitSurvey.do")
    @LogMethod(name="submit Survey")
    public ModelAndView submitSurvey(HttpServletRequest request,HttpServletResponse response) {

        try {
//            "pId~!@surveyId~!@item1~!@item2~!@item3~!@text"  ;
            List<SurveyAnswer> surveyAnswerList = new ArrayList<SurveyAnswer>();
            List<UserAnswer> userAnswerList = new ArrayList<UserAnswer>();


            String answers[] = request.getParameterValues("answers");

            if(answers!= null && answers.length>0){
                //处理每一道题
                for (String answer : answers) {
                    String[] surveyItems = answer.split("~!@");

                    String qId = surveyItems[0];
                    String sId = surveyItems[1];
                    String answerStr = surveyItems[2];

                    //添加到用户答题
                    userAnswerList.add(new UserAnswer(Integer.parseInt(qId),Integer.parseInt(sId),answerStr));


                    String[] answerList = answerStr.split("@!~");
                    //添加到每个题和选项的映射
                    for (int i = 0; i < answerList.length; i++) {

                        if(StringUtil.isInteger(answerList[i])) {
                            surveyAnswerList.add(new SurveyAnswer(Integer.parseInt(qId),
                                    Integer.parseInt(sId),
                                    Integer.parseInt(answerList[i])
                                    ,null));
                        } else {
                            surveyAnswerList.add(new SurveyAnswer(Integer.parseInt(qId),
                                    Integer.parseInt(sId),
                                    null,
                                    answerList[i]));
                        }
                    }
                }
                surveyService.submitSurvey(surveyAnswerList,userAnswerList);
            }

            JSONModel jsonModel = JSONModel.create();
            return createJSONModelAndView(jsonModel);
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

}
